﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace CADDemo2025
{
    public static class BmBlcokMethod
    {
        public static bool CreatBlock<T>(this List<T> entities, string name, Point3d point, out BlockTableRecord btr) where T : Entity
        {
            btr = new BlockTableRecord();
            btr.Origin = point;
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForWrite) as BlockTable;
                if (bt.Has(name)) return false;
                foreach (var item in entities)
                {
                    btr.AppendEntity(item.Clone() as Entity);
                }
                btr.Name = name;
                bt.Add(btr);
                tran.AddNewlyCreatedDBObject(btr, true);
                tran.Commit();
            }
            return true;
        }
        public static void DeleteBlock(this ObjectId id)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr = id.GetObject(OpenMode.ForWrite) as BlockTableRecord;
                if (btr != null)
                {
                    btr.Erase();
                }
                tran.Commit();
            }
        }

    }
}
